crimeDat <- read.csv("usaCrimes.csv")
head(crimeDat)
## X state year pop assault burglary rape theft vehicleTheft
## 1 1 alabama 1969 3411770 18.80256 69.05213 1.447929 107.4047 17.71808
## 2 2 alabama 1970 3454557 21.45861 77.40211 1.843941 118.0441 22.27782
## 3 3 alabama 1971 3497349 21.85941 78.76537 1.890003 116.7770 22.00524
## 4 4 alabama 1972 3540003 20.99151 78.28807 1.864405 108.7372 19.33897
## 5 5 alabama 1973 3580759 23.35259 88.67952 2.097321 109.4908 22.45055
## 6 6 alabama 1974 3627778 23.23461 104.30903 2.235528 129.0404 25.69617
## murder robbery
## 1 1.421550 4.244131
## 2 1.169470 5.010773
## 3 1.498278 5.732914
## 4 1.401129 6.799429
## 5 1.306985 7.844706
## 6 1.477488 9.818682
b. Generate a scatterplot showing murder vs rape for all states
ggplot(crimeDat, aes(rape, murder)) +
geom_point()
c. Show the same plot as you did in (a) for year 1981 and point size proportional to the popuation.
ggplot(subset(crimeDat, year==1981), aes(rape, murder)) +
geom_point(aes(size = sqrt(pop/pi)), pch = 21)
d. Show the same plot as you did in (b) for year 1981 and fill the points with color proportional to robbery.
p <- ggplot(subset(crimeDat, year==1981), aes(rape, murder, color=robbery, fill = robbery)) +
geom_point(aes(size = sqrt(pop/pi)), pch = 21)
p
e. Convert the plot in (d) into an interactive plot to show the state names. Which state shows most robbery or murder or rape?
library(plotly)
ggplotly(p + aes(label = state))
f. In (e) we generated the plot for only 1981. We want to see the same plot over all the years. For this generate an animated movie over years to show how the situations change over time. Which state do you think shows the most changes.
p <- ggplot(crimeDat, aes(rape, murder, frame=year, color=robbery, fill = robbery)) +
geom_point(aes(size = sqrt(pop/pi)), pch = 21) +
scale_size_continuous(range=c(1,8)) +
scale_fill_gradient(low = "blue", high = "hotpink") +
scale_color_gradient(low = "blue", high = "hotpink")
ggplotly(p + aes(label = state))
g. Show the state population of 1981 on USA state map using color.
library(ggmap)
library(dplyr)
maps <- map_data("state")
crime1981 <- subset(crimeDat, year==1981) %>%
mutate(state = as.character(state))
pdat <- left_join(maps, crime1981, by=c('region'='state')) %>%
arrange(order)
ggplot(pdat, aes(x=long, y=lat,group=group)) +
geom_polygon(aes(fill=pop), colour = alpha("white", 1/2), size = 0.2) + theme_bw() +
theme(legend.position = "none", text = element_blank(), line = element_blank()) +
scale_fill_continuous(low="blue", high="hotpink")
h. Show the state map as you did for 1981 for all the years in an animated movie. Which state shows the change in population the most over the years?
pdat <- left_join(maps, crimeDat, by=c('region'='state')) %>%
arrange(year, order)
p = ggplot(pdat, aes(x=long, y=lat,group=group, frame=year)) +
geom_polygon(aes(fill=pop), colour = alpha("white", 1/2), size = 0.2) + theme_bw() +
theme(legend.position = "none", text = element_blank(), line = element_blank()) +
scale_fill_continuous(low="blue", high="hotpink")
ggplotly(p)